package com.redis.springtest;

import com.redis.springtest.config.RedisConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class RedisExpireTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(RedisConfig.class);
        final RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);

        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations ops) throws DataAccessException {
                ops.boundValueOps("key1").set("value1");
                String keyValue = (String) ops.boundValueOps("key1").get();
                Long expire = ops.getExpire("key1");
                System.out.println(expire);
                boolean b = false;
                b = ops.expire("key1", 120L, TimeUnit.SECONDS);
                b = ops.persist("key1");
                Long l = 0L;
                l = ops.getExpire("key1");
                Long now = System.currentTimeMillis();
                Date date = new Date();
                date.setTime(now + 120_000);
                ops.expireAt("key1", date);
                return null;
            }
        });

    }
}
